//
//  NewFeatureViewController.swift
//  Weibo
//
//  Created by mac on 15/11/29.
//  Copyright © 2015年 zsl. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
private let maxImageCount = 4

class NewFeatureViewController: UICollectionViewController {
    init(){
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        //设置cell的大小
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        //关闭弹簧效果
        collectionView?.bounces = false
        //关闭collectionView的指示器
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier:reuseIdentifier )
        
    }
    
     //mark:实现collectionView的数据源方法
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return maxImageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        //cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.purpleColor() : UIColor.blueColor()
        cell.index = indexPath.item
    
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        print(indexPath.item)
        let cell = collectionView.visibleCells().last as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)
        let indexItem = index?.item ?? 0
        //如果是第三张图片的时候
        if indexItem == maxImageCount - 1 {
            cell.startAnimation()
        }
    }

}


//懒加载子视图  自定义cell
class NewFeatureCell: UICollectionViewCell{
    
    //定义index属性
    var index: Int = 0 {
        
        didSet{
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            startBtn.hidden = index != 3
            
        }
        
    }
    
    //重写初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     //mark:设置视图布局
 private func setupUI(){
    //添加子视图
    contentView.addSubview(iconView)
    contentView.addSubview(startBtn)
    
    //设置布局
    //snp_makeConstraints添加约束
   iconView.snp_makeConstraints { (make) -> Void in
    make.edges.equalTo(contentView.snp_edges)
    
    }
    
    startBtn.snp_makeConstraints { (make) -> Void in
        
        make.centerX.equalTo(contentView.snp_centerX)
        //添加底部的约束
        make.bottom.equalTo(contentView.snp_bottom).offset(-180)
    }
    
    //添加点击事件
    startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    }
  private  func startAnimation(){
        
        //动画开始之前隐藏按钮
    startBtn.hidden = false
    //设置动画的初始形变
    startBtn.transform = CGAffineTransformMakeScale(0, 0)
    
    
  /*
    iOS 7.0 推出的动画效果
    - duartion                          持续事件
    - parameter delay:                  延迟时间
    - parameter usingSpringWithDamping: 弹簧效果 0.1 ~ 1之间  越小越弹
    - parameter initialSpringVelocity:  加速度
    - parameter options:                动画可选项
    - parameter animations:             完成动画闭包
    - parameter completion:             动画结束后的闭包
    */
    UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
        
        self.startBtn.transform = CGAffineTransformIdentity
        }) { (_) -> Void in
            
            print("OK")
    }
    
        
    }
    
    /// 实现点击事件
    @objc private func startBtnDidClick(){
        
        print(__FUNCTION__)
    
    }
    //懒加载子视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1") )
    private lazy var startBtn: UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState:.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Normal)
        btn.setTitle("开始体验", forState: .Normal)
        
        //设置大小
        btn.sizeToFit()
        
        return btn
        
    }()
    
}

